package pack

import (
	"mall_admin_api/biz/model"
	"mall_admin_api/biz/model/admin_orders"
)

func GetOrderList(list []*model.Order, userMap map[int64]*model.User) []*admin_orders.Order {
	var data = make([]*admin_orders.Order, 0)
	if len(list) == 0 {
		return data
	}
	for _, category := range list {
		if category != nil {
			data = append(data, Order(category, userMap))
		}
	}
	return data
}

func Order(data *model.Order, userMap map[int64]*model.User) *admin_orders.Order {
	var info = &admin_orders.Order{}
	if data == nil {
		return info
	}
	info.ID = data.ID
	info.OrderNumber = data.OrderNumbers
	info.Status = data.Status
	info.PayStatus = data.PayStatus
	info.TotalPrice = data.TotalPrice
	info.UserID = data.UserID
	if !data.PayTime.IsZero() {
		info.PayTime = data.PayTime.UnixMilli()
	}
	if !data.SubmitTime.IsZero() {
		info.SubmitTime = data.SubmitTime.UnixMilli()
	}

	if userMap != nil {
		if u, ok := userMap[data.UserID]; ok {
			info.Nickname = GetUserName(u)
		}
	}

	return info
}

func OrderDetail(data *model.Order, orderItemList []*model.OrdersItem) *admin_orders.OrderDetail {
	var info = &admin_orders.OrderDetail{}
	if data == nil {
		return info
	}
	info.ID = data.ID
	info.OrderNumber = data.OrderNumbers
	info.Status = data.Status
	info.PayStatus = data.PayStatus
	info.TotalPrice = data.TotalPrice
	info.UserID = data.UserID
	if !data.PayTime.IsZero() {
		info.PayTime = data.PayTime.UnixMilli()
	}
	if !data.SubmitTime.IsZero() {
		info.SubmitTime = data.SubmitTime.UnixMilli()
	}
	info.ReceiverProvince = data.ReceiverProvince
	info.ReceiverCity = data.ReceiverCity
	info.ReceiverRegion = data.ReceiverRegion
	info.ReceiverAddress = data.ReceiverAddress
	info.ReceiverName = data.ReceiverName
	info.ReceiverEmail = data.ReceiverEmail
	info.ReceiverMobile = data.ReceiverMobile
	info.DeliveryNumber = data.DeliveryNumber
	var orderItem = make([]*admin_orders.OrderItem, 0)
	for _, item := range orderItemList {
		var temp = &admin_orders.OrderItem{}
		temp.ID = item.ID
		temp.GoodsName = item.ProductName
		temp.GoodsIcon = item.ProductIcon
		temp.Price = item.Price
		temp.Number = item.ProductNumber
		temp.TotalPrice = item.Price * float64(item.ProductNumber)
		orderItem = append(orderItem, temp)
	}

	info.OrderItem = orderItem
	info.Remark = data.Remark

	return info
}
